 <template>
  <div class="page">
    <s-header :name="'公示办理'" :back="pathName"></s-header>
    <div class="card-box">
      <div class="card-item">
        <div class="card-header">
          <div class="header-title">建房申请单号：{{ buildDataForm.applyOrderNo }}</div>
        </div>
        <van-divider />
        <div class="card-body card-body-nop">
          <van-cell title="申请人" :value="buildDataForm.applyName" />
          <van-cell title="申请时间" :value="buildDataForm.applyTime" />
          <van-cell title="联系电话" :value="buildDataForm.applyPhone" />
          <van-cell title="申请理由" :value="buildDataForm.applyReasons" />
          <van-cell title="建房类型" :value="buildDataForm.buildTypeName" />
          <van-cell title="家庭住址" :value="buildDataForm.applyHouseAddress" />
        </div>
      </div>

      <div class="card-item">
        <div class="card-header">
          <div class="header-title">公示异议</div>
        </div>
        <van-divider />
        <div :class="`card-body ${publicDissentVoList.length > 0 ? 'card-body-nop' : ''}`">
          <div v-if="publicDissentVoList.length > 0">
            <div v-for="(item, index) in publicDissentVoList" :key="index">
              <van-cell title="异议对象" :value="item.dissentObject" />
              <van-cell title="异议提出时间" :value="item.dissentTime" />
              <van-cell title="异议提出人" :value="item.dissentOriginator" />
              <van-cell title="身份证号" :value="item.dissentIdcard" />
              <van-cell title="联系电话" :value="item.dissentPhone" />
              <van-cell title="异议内容" :value="item.dissentContent" />
              <van-divider />
            </div>
          </div>
          <div v-else>无</div>
        </div>
      </div>

      <van-form ref="dataFormRef">
        <div class="card-item" v-if="publicDissentVoList.length > 0">
          <div class="card-header">
            <div class="header-title title-required">公示审批</div>
          </div>
          <van-divider />
          <div class="card-body">
            <van-radio-group v-model="dataForm.reviewResult" :rules="dataRule.reviewResult">
              <van-radio class="radio" name="04">异议不成立</van-radio>
              <van-radio class="radio" name="05">异议成立，退回至申请人</van-radio>
              <van-radio class="radio" name="99">挂起</van-radio>
            </van-radio-group>
          </div>
        </div>

        <div class="card-item">
          <div class="card-header">
            <div class="header-title title-required">办理意见</div>
          </div>
          <van-divider />
          <div class="card-body">
            <van-field
              v-model="dataForm.handleOpinions"
              :rules="dataRule.handleOpinions"
              class="noPadding"
              label=""
              rows="3"
              autosize
              type="textarea"
              placeholder="请输入办理意见"
            />
          </div>
        </div>
      </van-form>

      <div class="card-item">
        <div class="card-header">
          <div class="item-head">
            <div class="title title-required">公示文件</div>
            <!-- <div class="text-desc">（请上传会议纪要和签到表）</div> -->
          </div>
        </div>
        <van-divider />
        <div class="card-body">
          <image-upload v-model:value="publicFileList" fileType="004" @uploadBack="uploadBack"></image-upload>
        </div>
      </div>
      <div class="card-item">
        <div class="card-header">
          <div class="item-head">
            <div class="title">其他附件</div>
          </div>
        </div>
        <van-divider />
        <div class="card-body">
          <image-upload v-model:value="otherFileList" fileType="005" @uploadBack="uploadBack"></image-upload>
        </div>
      </div>
    </div>

    <div class="btn-group">
      <van-button type="button" class="cus-btn btn-default" @click="handleSave">保存</van-button>
      <van-button :loading="isCommit" type="button" class="cus-btn btn-primary" @click="handleTransact">提交</van-button>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted, getCurrentInstance, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { Toast } from 'vant'
import sHeader from '@/components/SimpleHeader'
import ImageUpload from '@/components/ImageUpload'
import { getNoticeDetail, savePublicVillage } from '@/service/govern'
export default {
  components: { sHeader, ImageUpload },
  setup() {
    const route = useRoute()
    const router = useRouter()
    const store = useStore()
    const dataFormRef = ref(null)
    const state = reactive({
      pathName: route.query.pathName,
      isCommit: false,
      dataForm: {
        reviewResult: '04',
        handleOpinions: '',
      },
      buildDataForm: {
        applyOrderNo: '',
        applyName: '',
        applyTime: '',
        applyPhone: '',
        applyReasons: '',
        buildTypeName: '',
        applyHouseAddress: '',
      },
      publicDissentVoList: [],
      publicFileList: [],
      otherFileList: [],
      dataRule: {
        handleOpinions: [{ required: true, message: '请输入办理意见', trigger: 'onBlur' }],
        reviewResult: [{ required: true, message: '请选择审查结果', trigger: 'onChange' }],
      },
    })

    // 全局属性
    const { listAssign } = getCurrentInstance().appContext.config.globalProperties
    store.dispatch('getFileServerPrefix')

    onMounted(async () => {
      const { id } = route.params
      if (id && id != 'undefined') {
        getNoticeDetail(id).then((res) => {
          const data = res.data
          state.publicDissentVoList = data.publicDissentVoList || []

          listAssign(state.buildDataForm, data)
          listAssign(state.dataForm, data)

          let publicFileList = data.publicFileList
          let otherFileList = data.otherFileList
          if (publicFileList && publicFileList.length > 0) {
            publicFileList.forEach((item) => {
              if (item.url.indexOf(fileServerPrefix.value) === -1) {
                item.filePath = item.url
                item.url = fileServerPrefix.value + item.url
              }
            })
          }
          if (otherFileList && otherFileList.length > 0) {
            otherFileList.forEach((item) => {
              if (item.url.indexOf(fileServerPrefix.value) === -1) {
                item.filePath = item.url
                item.url = fileServerPrefix.value + item.url
              }
            })
          }
          state.publicFileList = publicFileList
          state.otherFileList = otherFileList
        })
      } else {
        router.push({ path: `/government/review-public` })
      }
    })

    // 文件上传后回调
    const uploadBack = (fileList, type) => {
      if (type === '004') {
        state.publicFileList = fileList
      } else if (type === '005') {
        state.otherFileList = fileList
      }
    }

    const handleSave = () => {
      sendRequest('0')
    }

    const handleTransact = () => {
      dataFormRef.value.validate().then(() => {
        if (state.publicFileList.length === 0) {
          Toast.fail(`请上传公示文件`)
          return
        }
        sendRequest('1')
      })
    }

    const sendRequest = (isSubmit) => {
      let publicFileList = JSON.parse(JSON.stringify(state.publicFileList))
      let otherFileList = JSON.parse(JSON.stringify(state.otherFileList))
      publicFileList.forEach((item, index) => {
        item.sort = index
        item.url = item.filePath ? item.filePath : item.url
      })
      otherFileList.forEach((item, index) => {
        item.sort = index
        item.url = item.filePath ? item.filePath : item.url
      })

      if (state.publicDissentVoList.length === 0) {
        state.dataForm.reviewResult = '04'
      }

      state.isCommit = true
      let params = state.dataForm
      params.attachInfoDTOList = [...publicFileList, ...otherFileList]
      params.applyBaseId = route.params.id
      params.type = isSubmit
      savePublicVillage(params)
        .then(() => {
          Toast.success(`${isSubmit === '1' ? '提交' : '保存'}成功`)
          setTimeout(() => {
            router.push({ path: `/government/review-public` })
          }, 500)
          state.isCommit = false
        })
        .catch(() => {
          state.isCommit = false
        })
    }

    const fileServerPrefix = computed(() => {
      return store.state.fileServerPrefix
    })

    return {
      dataFormRef,
      fileServerPrefix,
      ...toRefs(state),
      handleTransact,
      handleSave,
      uploadBack,
    }
  },
}
</script>

<style lang="less" scoped>
@import '@/common/style/mixin';
.page {
  color: #2e3f6a;
  font-weight: 400;
  .card-box {
    margin-top: 10px;
    .van-divider {
      margin: 10px 0 0;
    }
    .card-header {
      font-size: 15px;
      .item-head {
        .fj();
        .text-add {
          font-weight: 400;
          color: #3487f6;
        }
        .text-desc {
          font-size: 13px;
          font-weight: 400;
          color: #9297ae;
        }
      }
      .title-required::before {
        margin-right: 5px;
        color: var(--van-field-required-mark-color);
        content: '*';
      }
    }
    .card-body {
      //   padding-bottom: 10px;
      .radio {
        margin-bottom: 12px;
      }
    }
    .last-item {
      margin-bottom: 0;
    }
  }
  .btn-group {
    padding: 30px 0 20px;
    .cus-btn {
      width: 167px;
    }
  }
}
</style>
 